﻿<!DOCTYPE html>
<html>
<head>
    <title>JS Objects Exercise 5</title>
    <link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
    <h1>Function that finds the youngest person in a given array of persons and prints his/hers full name.</h1>
    <div id="js-console"></div>
    <script src="scripts/js-console.js"></script>
    <script>
        function printYoungest(pers) {
            if (pers.length > 0) {
                youngestIdx = 0;
                for (var idx = 1; idx < pers.length; idx++) {
                    if (pers[idx].age < pers[youngestIdx].age)
                    {
                        youngestIdx = idx;
                    }
                }
                jsConsole.writeLine("Youngest: " + pers[youngestIdx].firstname + " " + pers[youngestIdx].lastname);
            }

        }

        var persons = [
          { firstname: "Gosho", lastname: "Petrov", age: 32 },
          { firstname: "Sanimir", lastname: "Hristov", age: 21 },
          { firstname: "Ceco", lastname: "Elvisa", age: 29 },
          { firstname: "Joro", lastname: "Hubaveca", age: 50 },
          { firstname: "Bay", lastname: "Ivan", age: 81 }
        ];

        printYoungest(persons);

    </script>
</body>
</html>
